package cn.example.zjgsu.service.Impl;

import cn.example.zjgsu.dao.CommentMapper;
import cn.example.zjgsu.dao.UserMapper;
import cn.example.zjgsu.pojo.Comment;
import cn.example.zjgsu.pojo.UserBriefInformation;
import cn.example.zjgsu.pojo.UserComment;
import cn.example.zjgsu.service.CommentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author xuejiahui
 * @date 2020/3/28 -20:16
 */
@Service
public class CommentServerImpl implements CommentServer {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;


    @Override
    public String  insertShopComment(MultipartFile[] files, HttpServletRequest request, String content, int shopid, String userid) throws IOException {
        if (files==null){
            System.out.println("files 是null");
            Date date = new Date(System.currentTimeMillis());
            String formatdate = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(date);
            Comment comment = new Comment();
            comment.setPraisenumber(0);
            comment.setTime(formatdate);
            comment.setUserid(userid);
            comment.setShopid(shopid);
            comment.setContent(content);
            commentMapper.insert(comment);
            return "插入成功";
        }else {
            System.out.println("files 不是Null");
            String pictureurl="";
            //获取文件名 : file.getOriginalFilename();
            System.out.println("文件长度"+files.length);
            for (int i = 0; i <files.length ; i++) {
                System.out.println(files[i].getName());
                String uploadFileName = files[i].getOriginalFilename();
                //如果文件名为空，直接回到首页！  
                if ("".equals(uploadFileName)) {
                    return "上传的图片文件名不能为空";
                }
                System.out.println("上传文件名 : " + uploadFileName);
                //上传路径保存设置  
                String path = ResourceUtils.getURL("classpath:").getPath()+"static/comment/";
                //如果路径不存在，创建一个  
                File realPath = new File(path);
                if (!realPath.exists()) {
                    realPath.mkdir();
                }
                System.out.println("上传文件保存地址：" + realPath);
                InputStream is = files[i].getInputStream();
                //文件输入流  
                OutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); //文件输出流
                //读取写出  
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                    os.flush();
                }
                os.close();
                is.close();
                if (i==files.length-1){
                    pictureurl=pictureurl+"http://10.21.234.73:8080/comment/"+uploadFileName;
                }else{
                    pictureurl=pictureurl+"http://10.21.234.73:8080/comment/"+uploadFileName+";";
                }
            }
            Date date = new Date(System.currentTimeMillis());
            String formatdate = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(date);
            Comment comment = new Comment();
            comment.setPraisenumber(0);
            comment.setTime(formatdate);
            comment.setUserid(userid);
            comment.setShopid(shopid);
            comment.setContent(content);
            System.out.println("pictureurl"+pictureurl);
            comment.setPictures(pictureurl);
            commentMapper.insert(comment);
            return "插入成功";
        }
    }

    @Override
    public String insertShopComment2(MultipartFile file, MultipartFile file1, MultipartFile file2, HttpServletRequest request, String content, int shopid, String userid) throws IOException {
        if (file!=null||file1!=null||file2!=null){
            String pictureurl="";
            if (file!=null){
                System.out.println("file正在插入");
                //获取文件名 : file.getOriginalFilename();    
                String uploadFileName = file.getOriginalFilename();
                //如果文件名为空，直接回到首页！  
                if ("".equals(uploadFileName)) {
                    return "redirect:/index.html";
                }
                System.out.println("上传文件名 : " + uploadFileName);
                //上传路径保存设置  
                String path = ResourceUtils.getURL("classpath:").getPath()+"static/file/";
                //如果路径不存在，创建一个  
                File realPath = new File(path);
                if (!realPath.exists()) {
                    realPath.mkdir();
                }
                System.out.println("上传文件保存地址：" + realPath);
                InputStream is = file.getInputStream();
                //文件输入流  
                OutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); //文件输出流
                //读取写出  
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                    os.flush();
                }
                os.close();
                is.close();
                pictureurl=pictureurl+"http://10.21.234.73:8080/comment/"+uploadFileName+";";
            }
            if (file1!=null){
                System.out.println("file1正在插入");
                //获取文件名 : file.getOriginalFilename();    
                String uploadFileName = file1.getOriginalFilename();
                //如果文件名为空，直接回到首页！  
                if ("".equals(uploadFileName)) {
                    return "redirect:/index.html";
                }
                System.out.println("上传文件名 : " + uploadFileName);
                //上传路径保存设置  
                String path = ResourceUtils.getURL("classpath:").getPath()+"static/file/";
                //如果路径不存在，创建一个  
                File realPath = new File(path);
                if (!realPath.exists()) {
                    realPath.mkdir();
                }
                System.out.println("上传文件保存地址：" + realPath);
                InputStream is = file1.getInputStream();
                //文件输入流  
                OutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); //文件输出流
                //读取写出  
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                    os.flush();
                }
                os.close();
                is.close();
                pictureurl=pictureurl+"http://10.21.234.73:8080/comment/"+uploadFileName+";";
            }
            if (file2!=null){
                System.out.println("file2正在插入");
                //获取文件名 : file.getOriginalFilename();    
                String uploadFileName = file2.getOriginalFilename();
                //如果文件名为空，直接回到首页！  
                if ("".equals(uploadFileName)) {
                    return "redirect:/index.html";
                }
                System.out.println("上传文件名 : " + uploadFileName);
                //上传路径保存设置  
                String path = ResourceUtils.getURL("classpath:").getPath()+"static/file/";
                //如果路径不存在，创建一个  
                File realPath = new File(path);
                if (!realPath.exists()) {
                    realPath.mkdir();
                }
                System.out.println("上传文件保存地址：" + realPath);
                InputStream is = file2.getInputStream();
                //文件输入流  
                OutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); //文件输出流
                //读取写出  
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                    os.flush();
                }
                os.close();
                is.close();
                pictureurl=pictureurl+"http://10.21.234.73:8080/comment/"+uploadFileName+";";

            }
            pictureurl=pictureurl.substring(0,pictureurl.length()-1);
            Date date = new Date(System.currentTimeMillis());
            String formatdate = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(date);
            Comment comment = new Comment();
            comment.setPraisenumber(0);
            comment.setTime(formatdate);
            comment.setUserid(userid);
            comment.setShopid(shopid);
            comment.setContent(content);
            comment.setPictures(pictureurl);
            commentMapper.insert(comment);
            return "插入成功";
        }else {
            Date date = new Date(System.currentTimeMillis());
            String formatdate = new SimpleDateFormat("HH:mm:ss").format(date);
            Comment comment = new Comment();
            comment.setPraisenumber(0);
            comment.setTime(formatdate);
            comment.setUserid(userid);
            comment.setShopid(shopid);
            comment.setContent(content);
            commentMapper.insert(comment);
            return "插入成功";
        }
    }

    @Override
    public int insertFoodRecordComment(String content, int foodrecordid, String userid) {
        Comment comment = new Comment();
        Date date = new Date(System.currentTimeMillis());
        String formatdate = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(date);
        comment.setPraisenumber(0);
        comment.setTime(formatdate);
        comment.setUserid(userid);
        comment.setFoodrecordid(foodrecordid);
        comment.setContent(content);
        return commentMapper.insert(comment);
    }

    @Override
    public List<UserComment> getShopComment(int shopid) {
        List<Comment> comments = commentMapper.selectCommentsByShopId(shopid);
        List<UserComment> userComments = new ArrayList<>();
        for (Comment comment : comments) {
            UserComment userComment = new UserComment();
            String userid = comment.getUserid();
            UserBriefInformation userBriefInformation = userMapper.selectUserBriefByUserid(userid);
            userComment.setContent(comment.getContent());
            userComment.setId(comment.getId());
            if (comment.getPictures()!=null){
                userComment.setPictures(Arrays.asList(comment.getPictures().split(";")));
            }
            userComment.setPraisenumber(comment.getPraisenumber());
            userComment.setTime(comment.getTime());
            userComment.setUserBriefInformation(userBriefInformation);
            userComments.add(userComment);
        }
        return userComments;
    }

    @Override
    public List<UserComment> getFoodRecordComment(int foodrecordid) {
        List<Comment> comments = commentMapper.selectCommentsByFoodRecordId(foodrecordid);
        List<UserComment> userComments = new ArrayList<>();
        for (Comment comment : comments) {
            UserComment userComment = new UserComment();
            String userid = comment.getUserid();
            UserBriefInformation userBriefInformation = userMapper.selectUserBriefByUserid(userid);
            userComment.setContent(comment.getContent());
            userComment.setId(comment.getId());
            userComment.setPraisenumber(comment.getPraisenumber());
            userComment.setTime(comment.getTime());
            userComment.setUserBriefInformation(userBriefInformation);
            userComments.add(userComment);
        }
        return userComments;
    }

    @Override
    public int deleteComment(int commentid) {
        return commentMapper.deleteByPrimaryKey(commentid);
    }

    @Override
    public int praiseComment(int commentid) {
        return commentMapper.updatePraisenumberById(commentid);
    }

}
